import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:mtf/app/data/VideoEntity.dart';
import 'package:mtf/app/net/HttpManger.dart';
import 'package:mtf/app/routes/app_pages.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'dart:convert';

import '../../data/ClassifyBeanEntity.dart';
/**
 * GetX Template Generator - fb.com/htngu.99
 * */

class videoListController extends GetxController{
  RxList<TypeList> typeList=<TypeList>[].obs;
  RxList<ListVideoElement> articles=<ListVideoElement>[].obs;
  RxList<ListVideoElement> videoList = <ListVideoElement>[].obs;
  var page = 1;

  void reqVideoHome() async{
    var classifyId=Get.arguments["classifyId"];
    var resp= await HttpManager.reqClassifyHome(classifyId);
    if(resp.isSuccessData()){
      typeList.assignAll(resp.data!.typeList!);
      articles.assignAll(resp.data!.articles!);

    }
  }

  @override
  void onInit() {
    super.onInit();
    reqVideoHome();
  }


  // void refreshList(){
  //   page=1;
  //   reqVideo();
  // }
  //
  // void loadList(){
  //   page+=1;
  //   reqVideo();
  // }

  void reqVideo(int tabIndex,RefreshController refreshController) async {
    var resp= await HttpManager.reqVideoList(page,typeList[tabIndex].typeId.toString());
    if(resp.isSuccess()&&resp.data!=null){
      if(page==1){
        videoList.assignAll(resp.data!.list);
        refreshController.refreshCompleted();
      }else{
        videoList.addAll(resp.data!.list);
        if(resp.data!.list.isEmpty){
          refreshController.loadNoData();
        }else{
          refreshController.loadComplete();
        }
      }}else{
      if(page==1){
        videoList.assignAll([]);
      }
      refreshController.refreshCompleted();
      refreshController.loadComplete();
    }
  }

  void clickVideoDetail(ListVideoElement item){
    Get.toNamed(Routes.VIDEO_DETAIL,arguments: {
      "title": item.title,
      "time": item.updatedAt.toString(),
      "cover": item.imghead,
      "video":item.fileVideo,
      "content":item.content
    });
  }
}
